package com.example.demo.controller;

import com.example.demo.entity.UserEntity;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    //登录
    @RequestMapping("userLogin")
    @ResponseBody
    public int userLogin(UserEntity user, Model model, HttpServletRequest request) {
        UserEntity userEntity = userService.userLoginService(user);
        if (userEntity == null) {
            return 0;
        }/*else if (userEntity.getUflag() == 0){*/
        HttpSession session = request.getSession();
        session.setAttribute("userEntity",userEntity);
            if (userEntity.getUtipe() == 1){
                //用户
                return 1;
            }else {
                //管理员
                return 2;
            }
        /*}
        return 0;*/
    }

    //重名
    @RequestMapping("chong")
    @ResponseBody
    public int chong(UserEntity user,Model model) {
        UserEntity userEntity = userService.chongService(user);
        if (userEntity == null) {
            return 0;
        }else {
            return 1;
        }
    }
    @RequestMapping("main")
    public String mian(){
        return "index";
    }

    //注册
    @RequestMapping("userInsert")
    @ResponseBody
    public int userInsert(UserEntity user) {
        int i = userService.insertUserService(user);
        if (i == 1) {
            return 0;
        }else {
            return 1;
        }
    }

    //重新登录
    @RequestMapping("reLogin")
    public String reLogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return "index";
    }

    //跳个人中心
    @RequestMapping("toUserMsg")
    public String toUserMsg(HttpServletRequest request){
        HttpSession session = request.getSession();
        UserEntity userEntity = (UserEntity)session.getAttribute("userEntity");
        UserEntity userEntity1 = userService.queryUserById(userEntity.getUid());
        session.setAttribute("userEntity",userEntity1);
        return "userMsg";
    }

    //修改个人信息
    @RequestMapping("userUpdate")
    @ResponseBody
    public int userUpdate(UserEntity user,HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity userEntity = (UserEntity)session.getAttribute("userEntity");
        user.setUbalance(user.getUbalance() + userEntity.getUbalance());
        int i = userService.userUpdateService(user);
        if (i != 1){
            return 1;
        }else {
            UserEntity userEntity1 = userService.queryUserById(user.getUid());
            session.setAttribute("userEntity",userEntity1);
            return 0;
        }
    }

    @RequestMapping("indexController")
    public String indexController(){
        return "welcome";
    }

}
